package Z_TopicPattern;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author： pang-yun
 * @date： 2021-12-20 14:06
 * 功能目的 ：用于
 */

public class TopicConsumerB {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 拿到信道
        Channel channel = RabbitMQUtils.getChannel();
        //声明交换机 队列  以及 将其绑定
        channel.exchangeDeclare(TopicConst.EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        channel.queueDeclare(TopicConst.QUEUE_NAME_B,false,false,false,null);
        channel.queueBind(TopicConst.QUEUE_NAME_B,TopicConst.EXCHANGE_NAME,TopicConst.ROUTE_KEY_B);
        channel.queueBind(TopicConst.QUEUE_NAME_B,TopicConst.EXCHANGE_NAME,TopicConst.ROUTE_KEY_C);

        //----------成功失败 回调函数----------
        DeliverCallback deliverCallback= (consumerTag, message) -> {
            System.out.println("message : "+ new String(message.getBody())+ '\n'
            +"message :"+ message.getEnvelope().getRoutingKey());
        };
        CancelCallback cancelCallback =consumerTag -> {
            System.out.println("message Failed!!!");
        };

        //-------接收消息-------
        channel.basicConsume(TopicConst.QUEUE_NAME_B,true,deliverCallback,cancelCallback);
    }
}
